package com.quanyan.place.web;

import java.text.MessageFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.quanyan.api.APIResponse;
import com.quanyan.place.entity.vo.TbBizPlaceUnitVo;
import com.quanyan.place.service.ApiPlaceService;
import com.quanyan.place.service.PlaceLampControlService;

/**
 * Created by songfan on 17-5-12.
 */
@RestController
@RequestMapping("/admin/api/place/lamp")
public class PlaceLampController {
	
    private static final Logger logger = LoggerFactory.getLogger(PlaceLampController.class);
    private static final Logger lampLogger = LoggerFactory.getLogger("placeUnitLampLog");
    
    @Autowired
    private ApiPlaceService apiPlaceService;
    
    @Autowired
    private PlaceLampControlService placeLampControlService;
    
    /**
     * 记录场馆单元灯控日志信息
     * @param tbBizPlaceUnitVo
     * @return
     */
    @RequestMapping(value = "/addLampLog")
    public APIResponse<?> addLampLog(@RequestBody TbBizPlaceUnitVo tbBizPlaceUnitVo) {
    	try {
    		Assert.notNull(tbBizPlaceUnitVo.getPlaceId(), "场馆ID不能为空");
        	Assert.notNull(tbBizPlaceUnitVo.getCategoryId(), "运动类型ID不能为空");
        	Assert.notNull(tbBizPlaceUnitVo.getPlaceUintId(), "场馆单元ID不能为空");
        	Assert.notNull(tbBizPlaceUnitVo.getSwitchFlag(), "灯开关状态不能为空");
        	Assert.hasText(tbBizPlaceUnitVo.getResult(), "灯控接口调用返回结果为空");
        	
        	String request = MessageFormat.format("placeId:{0}, categoryId:{1}, unitId:{2}, lampStatus:{3}", tbBizPlaceUnitVo.getPlaceId().toString()
        			,tbBizPlaceUnitVo.getCategoryId().toString(),tbBizPlaceUnitVo.getPlaceUintId(),tbBizPlaceUnitVo.getSwitchFlag().toString());
        	lampLogger.info("场馆单元灯控日志--> Request: {}, Response: {}", request, tbBizPlaceUnitVo.getResult());
		} catch (Exception e) {
			logger.warn("记录灯控日志失败", e);
			return APIResponse.returnFail(e.getMessage());
		}
    	return APIResponse.returnSuccess();
   }
    
   /**
    * 查询场馆单元信息
    * @param TbBizPlaceUnitVo
    * @return
    */
    @RequestMapping(value = "/queryPlaceUnit")
    public APIResponse<?> queryPlaceUnit(@RequestBody TbBizPlaceUnitVo tbBizPlaceUnitVo) {
         try {
        	return  APIResponse.returnSuccess(placeLampControlService.queryPlaceUnitByCagetory(tbBizPlaceUnitVo));
		} catch (Exception e) {
			logger.warn("查询场馆单元信息失败", e);
			return APIResponse.returnFail(e.getMessage());
		}
    }
    
    /**
     * 校验场馆单元关灯
     * @param TbBizPlaceUnitVo
     * @return
     */
     @RequestMapping(value = "/checkPlaceUnit", method = RequestMethod.POST)
     public APIResponse<?> checkPlaceUnit(@RequestBody TbBizPlaceUnitVo tbBizPlaceUnitVo) {
          try {
        	  Assert.notNull(tbBizPlaceUnitVo.getPlaceUintId(), "场馆单元ID不能为空");
        	  
        	  placeLampControlService.checkPlaceUint(tbBizPlaceUnitVo);
        	  return APIResponse.returnSuccess();
 		} catch (Exception e) {
 			logger.warn("场馆单元关灯失败", e);
 			return APIResponse.returnFail(e.getMessage());
 		}
     }
}
